package database.store;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import user.profile.Flags;

import database.PrepareFiles;

public class StoreProfile {
	private static StoreProfile thisInstance;
	private Properties profile;
	private String login;
	
	public void setProfile(Properties profile) {
		login = profile.getProperty(Flags.LOGIN.getFlag());
		this.profile = profile;
	}
	
	public Properties getProfile() {
		return profile;
	}
	
	public void save() throws IOException {
		final String DEFAULT = "This is the file with the profile infomation about "
			+ login;
		File file = new File(getPath());
		new File(file.getParent()).mkdirs();
		
		PrepareFiles.prepareFile(file.getAbsolutePath(), DEFAULT);
		FileOutputStream out = new FileOutputStream(file);
		profile.storeToXML(out, DEFAULT);
		out.close();
	}
	
	private String getPath() {
		return "Database/"+login+"/"+login+".xml";
	}
	
	public static StoreProfile getInstance() {
		if(thisInstance == null) {
			thisInstance = new StoreProfile();
		}
		
		return thisInstance;
	}
	
	public void clear() {
		profile = null;
		login = null;
	}
}